package com.bj.util;

import java.util.ArrayList;
import java.util.Collection;

/**
 * This class is a extension implementation of java.util.ArrayList. It will be
 * used to contain paging data and provide three new attributes: pageCount -
 * total page count of result set totalRowCount - total row count of result set
 * currentPage - current page number
 */
public class PageList extends ArrayList {

    //分页条码长度 (可以定义)     1...{11 12 13 14 15 [16] 17 18 19 20 21}...100
    public int pageBarcodeLength = 10;

    //分页条码开始的索引 根据[总页数] [分页条码长度] [当前页] 计算
    public int start = 0;

    //分页条码结束的索引 根据[总页数] [分页条码长度] [当前页] 计算
    public int end = 0;

    // for(int i = pageList.start ; i <= pageList.end ; i++ ){

    //判断分页条码前方是否显示[...] true:显示 false:不显示
    public boolean header = true;

    //判断分页条码后方是否显示[...] true:显示 false:不显示
    public boolean footer = true;

    //总页数
    public int pageCount = 0;

    //当前页.从1开始.
    public int currentPage = 0;

    //总条数
    public long totalRowCount = 0;

    //每页条数
    public int pageSize = 0;

    public PageList() {
        super();
    }

    public PageList(Collection c) {
        super(c);
    }

    public PageList(int initialCapacity) {
        super(initialCapacity);
    }



    /**
     * @param page          当前页
     * @param pageSize      每页条数
     * @param totalRowCount 总条数
     */
    public PageList(int page, int pageSize, long totalRowCount) {
        super();
        this.currentPage = page;
        this.pageSize = pageSize;
        this.totalRowCount = totalRowCount;
        this.initialize();
    }

    /**
     * @param c             当前页数据
     * @param page          当前页
     * @param pageSize      每页条数
     * @param totalRowCount 总条数
     */
    public PageList(Collection c, int page, int pageSize, long totalRowCount) {
        super(c);
        this.currentPage = page;
        this.pageSize = pageSize;
        this.totalRowCount = totalRowCount;
        this.initialize();
    }

    /**
     * @param c                 当前页数据
     * @param page              当前页
     * @param pageSize          每页条数
     * @param totalRowCount     总条数
     * @param pageBarcodeLength 页码长度
     */
    public PageList(Collection c, int page, int pageSize, long totalRowCount, int pageBarcodeLength) {
        super(c);
        this.pageBarcodeLength = pageBarcodeLength;
        this.currentPage = page;
        this.pageSize = pageSize;
        this.totalRowCount = totalRowCount;
        this.initialize();

    }

    public void initialize() {

        if (this.currentPage < 1) {
            this.currentPage = 1;
        }

        if (pageSize < 1) {
            pageSize = 1;
        }

        if (totalRowCount < 1) {
            this.currentPage = 1;
            this.pageCount = 1;
        } else {
            int pageCount = (int) ((totalRowCount + pageSize - 1) / pageSize);
            this.setPageCount(pageCount);
        }

        this.setPageBarcodeLength(this.pageBarcodeLength);

    }

    public int getNextPage() {
        if (currentPage >= pageCount) {
            return pageCount;
        }
        return currentPage + 1;
    }

    public int getPreviousPage() {
        if (currentPage <= 1) {
            return 1;
        }
        return currentPage - 1;
    }

    //验证page合法性
    public static int validate(int page, int pageSize, long count) {

        if (page < 1) {
            return 1;
        }

        int _pageCount = (int) ((count + pageSize - 1) / pageSize);

        if (page > _pageCount) {
            page = _pageCount;
        }

        return page;
    }

    public void setPageBarcodeLength(int pageBarcodeLength) {

        this.pageBarcodeLength = pageBarcodeLength;
        int index = this.getCurrentPage();
        int max = this.getPageCount();
        int b = this.pageBarcodeLength / 2;
        int start = index - b;
        int end = index + (this.pageBarcodeLength - b);

        if (start <= 1) {
            start = 1;
            end = this.pageBarcodeLength + 1;
            if (end >= max) {
                end = max;
            }
        }
        if (end >= max) {
            start = max - this.pageBarcodeLength;
            end = max;
            if (start < 1) {
                start = 1;
            }
        }

        header = start > 1;
        footer = end < max;

        this.start = start;
        this.end = end;

    }

    /**
     * Set the total page count
     */
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    /**
     * Get the total page count
     */
    public int getPageCount() {
        return pageCount;
    }

    /**
     * Set the current page number
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * Get the current page count
     */
    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPagesize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * Set the TOTAL row count. It's NOT the row count of current page, which
     * can be achieved through method size().
     */
    public void setTotalRowCount(long totalRowCount) {
        this.totalRowCount = totalRowCount;
    }


    /**
     * Get the total row count
     */
    public long getTotalRowCount() {
        return totalRowCount;
    }

    public boolean getHeader() {
        return header;
    }

    public void setHeader(boolean header) {
        this.header = header;
    }

    public boolean getFooter() {
        return footer;
    }

    public void setFooter(boolean footer) {
        this.footer = footer;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }


}